{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lab-02-2 linear regression feed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "tf.set_random_seed(777)  # for reprducibilty"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Try to find value for W and b to compute y_data = x_data * W + b  \n",
    "# We know that W should be 1 and b should be 0\n",
    "# But let's TensorFlow figure it out \n",
    "W = tf.Variable(tf.random_normal([1]), name='weight')\n",
    "b = tf.Variable(tf.random_normal([1]), name='bias')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## X and Y data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#### Now we can use X and Y in place of x_data and y_data\n",
    "#### placeholders for a tensor that will be always fed using feed_dict\n",
    "#### See http://stackoverflow.com/questions/36693740/\n",
    "X = tf.placeholder(tf.float32, shape=[None])\n",
    "Y = tf.placeholder(tf.float32, shape=[None])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Our Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Out hypothesis XW+b\n",
    "hypothesis = X * W + b\n",
    "\n",
    "# cost/loss function\n",
    "cost = tf.reduce_mean(tf.square(hypothesis - Y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Minimize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)\n",
    "train = optimizer.minimize(cost)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prepare session"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Launch the graph in a session.\n",
    "sess = tf.Session()\n",
    "# Initializes global variables in the graph.\n",
    "sess.run(tf.global_variables_initializer())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fit the line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 3.52408 [ 2.12867713] [-0.85235667]\n",
      "200 0.0703044 [ 1.30721486] [-0.69837117]\n",
      "400 0.0268456 [ 1.18983996] [-0.4315508]\n",
      "600 0.010251 [ 1.11730957] [-0.26667204]\n",
      "800 0.00391432 [ 1.07249022] [-0.16478711]\n",
      "1000 0.00149468 [ 1.04479456] [-0.10182849]\n",
      "1200 0.00057074 [ 1.02768016] [-0.06292368]\n",
      "1400 0.000217935 [ 1.01710474] [-0.03888312]\n",
      "1600 8.32203e-05 [ 1.01056981] [-0.02402747]\n",
      "1800 3.17767e-05 [ 1.00653136] [-0.01484741]\n",
      "2000 1.21343e-05 [ 1.00403607] [-0.00917497]\n"
     ]
    }
   ],
   "source": [
    "for step in range(2001):\n",
    "    cost_val, W_val, b_val, _ = \\\n",
    "        sess.run([cost, W, b, train],\n",
    "                 feed_dict={X: [1, 2, 3], Y: [1, 2, 3]})\n",
    "    if step % 200 == 0:\n",
    "        print(step, cost_val, W_val, b_val)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Learns best fit W:[ 1.],  b:[ 0]\n",
    "```\n",
    "...\n",
    "1800 3.17767e-05 [ 1.00653136] [-0.01484741]\n",
    "2000 1.21343e-05 [ 1.00403607] [-0.00917497]\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Testing our model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 5.0110054]\n",
      "[ 2.50091505]\n",
      "[ 1.49687922  3.50495124]\n"
     ]
    }
   ],
   "source": [
    "print(sess.run(hypothesis, feed_dict={X: [5]}))\n",
    "print(sess.run(hypothesis, feed_dict={X: [2.5]}))\n",
    "print(sess.run(hypothesis, feed_dict={X: [1.5, 3.5]}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "[ 5.0110054]\n",
    "[ 2.50091505]\n",
    "[ 1.49687922  3.50495124]\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fit the line with new training data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1.20359 [ 1.06969857] [ 0.01276637]\n",
      "200 0.0499406 [ 1.14459527] [ 0.5779649]\n",
      "400 0.0128862 [ 1.07344985] [ 0.83482295]\n",
      "600 0.00332502 [ 1.03730989] [ 0.96529907]\n",
      "800 0.000857956 [ 1.01895225] [ 1.03157616]\n",
      "1000 0.000221378 [ 1.00962698] [ 1.06524324]\n",
      "1200 5.71206e-05 [ 1.00489008] [ 1.08234489]\n",
      "1400 1.47405e-05 [ 1.0024842] [ 1.09103119]\n",
      "1600 3.80434e-06 [ 1.00126207] [ 1.09544384]\n",
      "1800 9.81556e-07 [ 1.00064111] [ 1.09768546]\n",
      "2000 2.5373e-07 [ 1.00032604] [ 1.09882331]\n"
     ]
    }
   ],
   "source": [
    "for step in range(2001):\n",
    "    cost_val, W_val, b_val, _ = \\\n",
    "        sess.run([cost, W, b, train],\n",
    "                 feed_dict={X: [1, 2, 3, 4, 5],\n",
    "                            Y: [2.1, 3.1, 4.1, 5.1, 6.1]})\n",
    "    if step % 200 == 0:\n",
    "        print(step, cost_val, W_val, b_val)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Testing our model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 6.10045338]\n",
      "[ 3.59963846]\n",
      "[ 2.59931231  4.59996414]\n"
     ]
    }
   ],
   "source": [
    "print(sess.run(hypothesis, feed_dict={X: [5]}))\n",
    "print(sess.run(hypothesis, feed_dict={X: [2.5]}))\n",
    "print(sess.run(hypothesis, feed_dict={X: [1.5, 3.5]}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "1960 3.32396e-07 [ 1.00037301] [ 1.09865296]\n",
    "1980 2.90429e-07 [ 1.00034881] [ 1.09874094]\n",
    "2000 2.5373e-07 [ 1.00032604] [ 1.09882331]\n",
    "[ 6.10045338]\n",
    "[ 3.59963846]\n",
    "[ 2.59931231  4.59996414]\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
